/*
 * @Author: linzeqin
 * @Date: 2019-09-30 09:24:09
 * @description: 沙箱资源调度cnotroller
 */
import ResponseModel, { PageResponseModel } from '../model/responseModel';
import CommonController from '../abstract/controller/common';
import { roleAuthority } from '../abstract/controller/decorators';
import { UserRoleEnum } from "@/constants/enum.types";

export default class SandboxController extends CommonController {
    /** 统一鉴权 */
    /**
     * 列表查询
     * @memberof SandboxController
     */
    public async list() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = this.app.validator.validate({
            pageIndex: { type: 'number' },
            pageSize: { type: 'number' },
            name: { type: 'string', required: false },
            env: { type: 'string', required: false },
            createTime: { type: 'array', required: false },
            updateTime: { type: 'array', required: false },
        }, body);
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        const { pageIndex, pageSize, name, createTime, updateTime } = body;
        try {
            const res = await this.ctx.service.sandboxService.queryList({
                pageIndex: pageIndex as number,
                pageSize: pageSize as number,
                name: name || '',
                createTime: createTime || [],
                updateTime: updateTime || [],
            });
            this.ctx.body = new PageResponseModel({
                success: true,
                message: '查询成功',
                data: res,
            });
        } catch (error) {
            this.ctx.body = new PageResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 根据id查看项目详情
     * @memberof SandboxController
     */
    public async detail() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = this.app.validator.validate({
            id: { type: 'string' },
        }, body);
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        const { id } = body;
        try {
            const res = await this.ctx.service.sandboxService.detail(id);
            this.ctx.body = new ResponseModel({
                success: true,
                message: '查询成功',
                data: res,
            });
        } catch (error) {
            this.ctx.body = new ResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 新增or修改
     * @memberof SandboxController
     */
    @roleAuthority(UserRoleEnum.dev_ops)
    public async createOrModify() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = body.id
          ? this.app.validator.validate(
                {
                    id: { type: 'string' },
                    name: { type: 'string' },
                    scriptList: { type: 'array' },
                },
                body,
            )
          : this.app.validator.validate(
                {
                    name: { type: 'string' },
                    scriptList: { type: 'array' },
                },
                body,
            );
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        try {
            const res = await this.ctx.service.sandboxService.createOrModify({
                id: body.id,
                name: body.name,
                description: body.description,
                scriptList: body.scriptList,
                route: body.route,
            });
            this.ctx.body = new ResponseModel({
                success: true,
                data: res,
                message: !body.id ? '新增成功' : '修改成功',
            });
        } catch (error) {
            this.ctx.body = new ResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 删除
     * @memberof SandboxController
     */
    @roleAuthority(UserRoleEnum.dev_ops)
    public async delete() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = this.app.validator.validate({
            id: { type: 'string' },
        }, body);
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        const { id } = body;
        try {
            const res = await this.ctx.service.sandboxService.delete(id);
            this.ctx.body = new ResponseModel({
                success: true,
                message: '删除成功',
                data: res,
            });
        } catch (error) {
            this.ctx.body = new ResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 调度列表
     * @memberof SandboxController
     */
    public async dispatchList() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = this.app.validator.validate({
            pageIndex: { type: 'number' },
            pageSize: { type: 'number' },
            createTime: { type: 'array', required: false },
            updateTime: { type: 'array', required: false },
        }, body);
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        try {
            const { pageIndex, pageSize, createTime, updateTime, name } = body;
            const res = await this.ctx.service.sandboxService.dispatchList({
                pageIndex: pageIndex || 1,
                pageSize: pageSize || 10,
                name: name || '',
                createTime,
                updateTime,
            });
            this.ctx.body = new PageResponseModel({
                success: true,
                message: '查询成功',
                data: res,
            });
        } catch (error) {
            this.ctx.body = new PageResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 根据id查看项目详情
     * @memberof SandboxController
     */
    public async dispatchDetail() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = this.app.validator.validate({
            id: { type: 'string' },
        }, body);
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        const { id } = body;
        try {
            const res = await this.ctx.service.sandboxService.dispatchDetail(id);
            this.ctx.body = new ResponseModel({
                success: true,
                message: '查询成功',
                data: res,
            });
        } catch (error) {
            this.ctx.body = new ResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 新增or修改调度
     * @memberof SandboxController
     */
    @roleAuthority(UserRoleEnum.dev_ops)
    public async dispatchCreateOrModify() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = body.id
          ? this.app.validator.validate(
                {
                    id: { type: 'string' },
                    productId: { type: 'string' },
                },
                body,
            )
          : this.app.validator.validate(
                {
                    productId: { type: 'string' },
                },
                body,
            );
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        try {
            const res = await this.ctx.service.sandboxService.dispatchCreateOrModify({
                id: body.id,
                description: body.description,
                productId: body.productId,
            });
            this.ctx.body = new ResponseModel({
                success: true,
                data: res,
                message: !body.id ? '新增成功' : '修改成功',
            });
        } catch (error) {
            this.ctx.body = new ResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 删除调度
     * @memberof SandboxController
     */
    @roleAuthority(UserRoleEnum.super_admin)
    public async dispatchDelete() {
        const { ctx } = this;
        const { body } = ctx.request;
        /** 参数校验 */
        const errors = this.app.validator.validate({
            id: { type: 'string' },
        }, body);
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        const { id } = body;
        try {
            const res = await this.ctx.service.sandboxService.dispatchDelete(id);
            this.ctx.body = new ResponseModel({
                success: true,
                message: '删除成功',
                data: res,
            });
        } catch (error) {
            this.ctx.body = new ResponseModel({
                success: false,
                // @ts-ignore
                message: error && error.toString(),
            });
        }
    }

    /**
     * 根据调度序号和环境变量获取脚本内容
     * @memberof SandboxController
     */
    public async getScript() {
        const { ctx } = this;
        const { query } = ctx.request;
        const { dpNo, env } = query;
        this.ctx.set('Content-Type', 'text/javascript;charset=UTF-8');
        try {
            const res = await this.ctx.service.sandboxService.getScript(Number(dpNo), env);
            if (res && res.length > 0) {
                this.ctx.body = res[0].product.scriptList.script;
            } else {
                this.ctx.body = null;
            }
        } catch (error) {
            this.ctx.body = null;
        }
    }
}
